﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WordService.DBUtil;
using WordService.Model;
using WordService.Business;
using WordService.Business.IBLL;
using WordLib.ConfigUI;
using WordLib.ImportUI;
using WordLib.MatrixUI;
using WordLib.ProcessUI;
using WordService.Utility;

namespace WordLib
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.MinimizeBox = false;
            this.MaximizeBox = false;
        }

        private CalSettingsPanel matrixPanel;
        #region 私有方法，非按钮事件
        /// <summary>
        /// 私有方法，更新面板
        /// </summary>
        /// <param name="control"></param>
        private void UpdatePanelUI(UserControl control)
        {
            this.plMainPanel.Controls.Clear();
            this.plMainPanel.Controls.Add(control);
            this.plMainPanel.Update();
            if (control.GetType() == typeof(CalSettingsPanel))
            {
                this.ProcessToolStripMenuItem.Enabled = true;
            }
            else
            {
                this.ProcessToolStripMenuItem.Enabled = false;
            }
        }
        #endregion

        /// <summary>
        /// 退出菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 导入数据：单文件导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdatePanelUI(new FileReaderPanel());
        }
        /// <summary>
        /// 导入数据：文件夹导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdatePanelUI(new FolderReaderPanel());
        }
        /// <summary>
        /// 数据源配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SettingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdatePanelUI(new ConfigPanel());
        }
        /// <summary>
        /// 权值配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RightRateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdatePanelUI(new RightRatePanel());
        }
        /// <summary>
        /// 带权值关键字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeyWordPanel panel = new KeyWordPanel();
            panel.IsRate = true;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 不带权值关键字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NRKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeyWordPanel panel = new KeyWordPanel();
            panel.IsRate = false;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 带权值作者
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AuthorPanel panel = new AuthorPanel();
            panel.IsRate = true;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 不带权值作者
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NRAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AuthorPanel panel = new AuthorPanel();
            panel.IsRate = false;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 带权值机构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsititutionPanel panel = new InsititutionPanel();
            panel.IsRate = true;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 不带权值机构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NRIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InsititutionPanel panel = new InsititutionPanel();
            panel.IsRate = false;
            UpdatePanelUI(panel);
        }
        /// <summary>
        /// 针对设置内容的填充Panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CalSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            matrixPanel = new CalSettingsPanel();
            UpdatePanelUI(matrixPanel);
        }
        /// <summary>
        /// 关键字类别矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KeyWordCategoryMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            ShowSubForm("1.关键字类别矩阵", ProcessMatrixUtil.CalKCMatrix(sourceKeyword, selectKeyword), "对不起，请设置好类别以及它和关键字的对应关系过后再生成该矩阵！");
        }
        /// <summary>
        /// 关键字类别归一化矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KeyWordCategoryNMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            ShowSubForm("2.关键字类别归一化矩阵", ProcessMatrixUtil.CalKCNormalizeMatrix(sourceKeyword, selectKeyword), "对不起，请设置好类别以及它和关键字的对应关系过后再生成该矩阵！");
        }
        /// <summary>
        /// 作者相似度矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AuthorCategoryMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            IDictionary<String, IList<Int64>> selectAuthors = matrixPanel.GetSelectAuthors();
            ShowSubForm("3.作者相似度矩阵", ProcessMatrixUtil.CalACMatrix(sourceKeyword, selectKeyword, selectAuthors), "对不起，请设置好类别以及它的关键字以及作者，再生成该矩阵！");
        }

        private void AuthorCategoryNMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            IDictionary<String, IList<Int64>> selectAuthors = matrixPanel.GetSelectAuthors();
            ShowSubForm("4.作者相似度归一矩阵", ProcessMatrixUtil.CalACNormalizeMatrix(sourceKeyword, selectKeyword, selectAuthors), "对不起，请设置好类别以及它的关键字以及作者，再生成该相似矩阵！");
        }

        private void AuthorCategoryUMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            IDictionary<String, IList<Int64>> selectAuthors = matrixPanel.GetSelectAuthors();
            ShowSubForm("5.作者改进相似度矩阵", ProcessMatrixUtil.CalACUpgradeMatrix(sourceKeyword, selectKeyword, selectAuthors), "对不起，请设置好类别以及它的关键字以及作者，再生成该相似矩阵！");
        }

        private void ShowSubForm(String title, DataTable data, String msg)
        {
            if (matrixPanel != null)
            {
                IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
                IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
                if (sourceKeyword.Count != 0 && selectKeyword.Count != 0)
                {
                    MatrixSubForm form = new MatrixSubForm(title, data);
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show(this, msg, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void AuthorCategoryFMatrixItem_Click(object sender, EventArgs e)
        {
            IDictionary<String, IList<Keyword>> sourceKeyword = matrixPanel.GetKeywordSource();
            IDictionary<String, KeywordGrid> selectKeyword = matrixPanel.GetSelectKeywordSource();
            IDictionary<String, IList<Int64>> selectAuthors = matrixPanel.GetSelectAuthors();
            ShowSubForm("6.测试矩阵", ProcessMatrixUtil.CalACFinalMatrix(sourceKeyword, selectKeyword, selectAuthors), "对不起，请设置好类别以及它的关键字以及作者，再生成该相似矩阵！");
        }
    }
}
